#coding=UTF-8 
# from selenium.webdriver.common.action_chains import ActionChains

from chinacQA.TestLink_TestCase import assertBool
from chinacQA.TestLink_TestCase import getConfigure_CN
from chinacQA.TestLink_TestCase import waitMoment

from chinacQA.TestCase import TestCase
from template.NeoCUUI_CN import *
from TC_206_NeoCUServer import logonOut
from TC_206_NeoCUServer import STEP_deleteRubbish

def Login(user, pw):
    idusername.text=user
    idpassword.text=pw
    loginBtn.click()

def STEP_UserLogin():
    Login(getConfigure_CN("User"),getConfigure_CN("UserNewPassword"))

def STEP_createCinder():
    StorangeMenu_tab.click()
    CinderMenu.click()
    volumes_action_create.click()
    
    Cinderidname.text=getConfigure_CN("CinderName_1")
    iddescription.text=getConfigure_CN("CinderDes_1")
    idsize.text=getConfigure_CN("CinderSize_1")
    CreateCinderButton.click()
    waitMoment(3)
    heads = volumeTable.getHeads()
    columnName = heads.index('名称')
#     columnAction = heads.index('操作')
    for i in range(len(volumeTable.getRows())):
        if(volumeTable.getCell(i,columnName).text==getConfigure_CN("CinderName_1")):
            assertBool(True,"3343")
            return
    assertBool(False,"3343") 
 
def STEP_CinderEdit():
    CinderMenu.click()
    heads = volumeTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    for i in range(len(volumeTable.getRows())):
        if(volumeTable.getCell(i,columnName).text==getConfigure_CN("CinderName_1")):
            actioncell=volumeTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="编辑云硬盘"):
                    button.click()
                    CinderEditidname.text = getConfigure_CN("CinderNewName")
                    CinderEditiddescription.text = getConfigure_CN("CinderNewDes")
                    CinderEditCommit.click()
                    break
           
    status = False
    for i in range(len(volumeTable.getRows())):
        if (volumeTable.getCell(i,columnName)!=None and volumeTable.getCell(i,columnName).text.strip()== getConfigure_CN("CinderNewName")):
            status = True
    assertBool(status,"3345")        

def STEP_CinderManageConnect():
    CinderMenu.click()
    heads = volumeTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    columnStatus = heads.index('状态')
    for i in range(len(volumeTable.getRows())):
        if(volumeTable.getCell(i,columnName).text==getConfigure_CN("CinderNewName")):
            actioncell=volumeTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="管理连接"):
                    button.click()
                    break
    waitMoment(5)
    ChooseCinderConnect.click()
    menus = ChooseCinderConnect.element.find_elements("xpath",".//li")
    for buttonm in menus:
        if (getConfigure_CN("Server_Newname") in buttonm.text.strip()):
            buttonm.click()
            ConnectCommit.click()
            break
    for i in range(200):
        waitMoment(3)
        for i in range(len(volumeTable.getRows())):
            if (volumeTable.getCell(i,columnName)!=None and volumeTable.getCell(i,columnName).text.strip()== getConfigure_CN("CinderNewName")):
                if(volumeTable.getCell(i,columnStatus).text == "正在使用"):
                    assertBool(True,"3485")
                    return 
    assertBool(False,"3485")
          
def STEP_CinderManageDisConnect():
    CinderMenu.click()
    heads = volumeTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    columnStatus = heads.index('状态')
    for i in range(len(volumeTable.getRows())):
        if(volumeTable.getCell(i,columnName).text==getConfigure_CN("CinderNewName")):
            actioncell=volumeTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="管理连接"):
                    button.click()
                    break
                
    serverheads = volumeConnectTable.getHeads()            
    columnNameCinder = serverheads.index('云主机')
    columnActionCinder = serverheads.index('操作')
    for j in range(len(volumeConnectTable.getRows())):
        if(volumeConnectTable.getCell(j,columnNameCinder).text.strip()==getConfigure_CN("Server_Newname")):
            Newactioncell=volumeConnectTable.getCell(j,columnActionCinder)     
            Newactioncell.find_element("xpath",".//div").click()    
            DisconnectCommit.click()
            break
                
    for i in range(200):
        waitMoment(3)
        for i in range(len(volumeTable.getRows())):
            if (volumeTable.getCell(i,columnName)!=None and volumeTable.getCell(i,columnName).text.strip()== getConfigure_CN("CinderNewName")):
                if(volumeTable.getCell(i,columnStatus).text == "可用"):
                    assertBool(True,"3487")
                    return 
    assertBool(False,"3487")

def STEP_CinderCreateSnapShot():
    CinderMenu.click()
    heads = volumeTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    for i in range(len(volumeTable.getRows())):
        if(volumeTable.getCell(i,columnName).text==getConfigure_CN("CinderNewName")):
            actioncell=volumeTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="创建快照"):
                    button.click()
                    break
    Cinder2Snapshotidname.text = getConfigure_CN("Cinder_SnapshotName")            
    Cinder2SnapshotDec.text = getConfigure_CN("Cinder_SnapshotDec") 
    Cinder2SnapshotCommit.click()             
            
    waitMoment(3)
    for i in range(200):
        Menu_CinderSnapShot.click()
        waitMoment(3)
        nameHead = volumesnapshotsTable.getHeads().index("名称")
        for i in range(len(volumesnapshotsTable.getRows())):
            if (volumesnapshotsTable.getCell(i,nameHead)!=None and volumesnapshotsTable.getCell(i,nameHead).text.strip()== getConfigure_CN("Cinder_SnapshotName")):
                assertBool(True,"3351")
                return 
    assertBool(False,"3351")

def STEP_CinderDilatation():
    CinderMenu.click()
    heads = volumeTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    columnSize = heads.index('容量（GB）')
    for i in range(len(volumeTable.getRows())):
        if(volumeTable.getCell(i,columnName).text==getConfigure_CN("CinderNewName")):
            actioncell=volumeTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="云硬盘扩容"):
                    button.click()
                    break
            
    idnew_size.text = getConfigure_CN("CinderSize_NewSize")
    CinderDCommit.click()
    
    for i in range(200):
        waitMoment(3)
        for i in range(len(volumeTable.getRows())):
            if (volumeTable.getCell(i,columnName)!=None and volumeTable.getCell(i,columnName).text.strip()== getConfigure_CN("CinderNewName")):
                if(getConfigure_CN("CinderSize_NewSize") in volumeTable.getCell(i,columnSize).text):
                    assertBool(True,"3347")
                    return 
    assertBool(False,"3347")

def STEP_CinderAutoSnapshot():
    pass

def STEP_CinderCopy():
    CinderMenu.click()
    heads = volumeTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    for i in range(len(volumeTable.getRows())):
        if(volumeTable.getCell(i,columnName).text==getConfigure_CN("CinderNewName")):
            actioncell=volumeTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="复制云硬盘"):
                    button.click()
                    CopyCinderCommit.click()
                    break
            break
    for i in range(len(volumeTable.getRows())):
        if(volumeTable.getCell(i,columnName).text.strip()==(getConfigure_CN("CinderNewName")+"_copy")):
            assertBool(True,"3355")
            return
    assertBool(False,"3355")             
    
def STEP_SnapShotCreateCinder():
    Menu_CinderSnapShot.click()
    heads = volumesnapshotsTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    for i in range(len(volumesnapshotsTable.getRows())):
        if(volumesnapshotsTable.getCell(i,columnName).text==getConfigure_CN("Cinder_SnapshotName")):
            actioncell=volumesnapshotsTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="创建云硬盘"):
                    button.click()
                    break
            break
        
    SnapShot2Cindername.text = getConfigure_CN("SnapshotCinder_name")            
    Snapshot2Cinderdescription.text = getConfigure_CN("SnapshotCinder_dec") 
    Snapshot2Cinderidsize.text = getConfigure_CN("SnapshotCinder_size") 
    SnapShot2CinderCreate.click()             
            
    waitMoment(3)
    for i in range(200):
        waitMoment(3)
        nameHead = volumeTable.getHeads().index("名称")
        for i in range(len(volumeTable.getRows())):
            if (volumeTable.getCell(i,nameHead)!=None and volumeTable.getCell(i,nameHead).text.strip()== getConfigure_CN("SnapshotCinder_name")):
                assertBool(True,"3489")
                return 
    assertBool(False,"3489")

def STEP_SnapShotEdit():
    Menu_CinderSnapShot.click()
    heads = volumesnapshotsTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    for i in range(len(volumesnapshotsTable.getRows())):
        if(volumesnapshotsTable.getCell(i,columnName).text==getConfigure_CN("Cinder_SnapshotName")):
            actioncell=volumesnapshotsTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="编辑云硬盘快照"):
                    button.click()
                    break
            break
        
    SnapShot2Editidname.text = getConfigure_CN("SnapshotCinder_Newname")            
    Snapshot2Editiddescription.text = getConfigure_CN("SnapshotCinder_Newdec") 
    SnapshotEditCommit.click()             
            
    waitMoment(3)
    for i in range(200):
        waitMoment(3)
        nameHead = volumesnapshotsTable.getHeads().index("名称")
        for i in range(len(volumesnapshotsTable.getRows())):
            if (volumesnapshotsTable.getCell(i,nameHead)!=None and volumesnapshotsTable.getCell(i,nameHead).text.strip()== getConfigure_CN("SnapshotCinder_Newname")):
                assertBool(True,"3361")
                return 
    assertBool(False,"3361")

def STEP_SnapShotRollBack():
    Menu_CinderSnapShot.click()
    heads = volumesnapshotsTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    columnStatus = heads.index('状态')
    for i in range(len(volumesnapshotsTable.getRows())):
        if(volumesnapshotsTable.getCell(i,columnName).text.strip()==getConfigure_CN("SnapshotCinder_Newname")):
            actioncell=volumesnapshotsTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="回滚云硬盘快照"):
                    button.click()
                    RollBackButton.click()
                    break
            break         
            
    waitMoment(3)
    for i in range(200):
        waitMoment(3)
        for i in range(len(volumesnapshotsTable.getRows())):
            if (volumesnapshotsTable.getCell(i,columnName)!=None and volumesnapshotsTable.getCell(i,columnName).text.strip()== getConfigure_CN("SnapshotCinder_Newname")):
                if(volumesnapshotsTable.getCell(i,columnStatus).text.strip()=="可用"):
                    assertBool(True,"3363")
                    return 
    assertBool(False,"3363")

def STEP_Clean():
    CinderMenu.click()
    heads = volumeTable.getHeads()
    columnName = heads.index('名称')
    columnAction = heads.index('操作')
    for i in range(len(volumeTable.getRows())):
        if(volumeTable.getCell(i,columnName).text==getConfigure_CN("SnapshotCinder_name")):
            actioncell=volumeTable.getCell(i,columnAction)
            menu_ul = actioncell.find_element("xpath",".//div")
            menu_ul.click()
            menubuttons = menu_ul.find_elements("xpath",".//li")
            for button in menubuttons:
                if (button.text.strip()=="删除云硬盘"):
                    button.click()
                    DeleteCinderButton.click()
                    break
            break
    STEP_deleteRubbish()      
    

TC_207_NeoCUStorage = TestCase("Neocu 2.5 CN Storage")
TC_207_NeoCUStorage.AddStep(STEP_UserLogin)
TC_207_NeoCUStorage.AddStep(STEP_createCinder)
TC_207_NeoCUStorage.AddStep(STEP_CinderEdit)
TC_207_NeoCUStorage.AddStep(STEP_CinderManageConnect)
TC_207_NeoCUStorage.AddStep(STEP_CinderManageDisConnect)
TC_207_NeoCUStorage.AddStep(STEP_CinderCreateSnapShot)
TC_207_NeoCUStorage.AddStep(STEP_CinderDilatation)
TC_207_NeoCUStorage.AddStep(STEP_CinderCopy)
TC_207_NeoCUStorage.AddStep(STEP_SnapShotCreateCinder)
TC_207_NeoCUStorage.AddStep(STEP_SnapShotEdit)
TC_207_NeoCUStorage.AddStep(STEP_SnapShotRollBack)
TC_207_NeoCUStorage.AddStep(STEP_Clean)
TC_207_NeoCUStorage.AddStep(logonOut)

if __name__ == "__main__":
    TC_207_NeoCUStorage.run()
